//
//  DetailViewController.m
//  UI16.真.豆瓣
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 Amydom. All rights reserved.
//

#import "ActDetailViewController.h"
#import "ActDetailCell.h"
#import "AppTools.h"
#import "UIImageView+WebCache.h"
#import "LoginViewController.h"
#import "SingLeton.h"
#import "ActiveModel.h"
#import "MBProgressHUD.h"
@interface ActDetailViewController ()<MBProgressHUDDelegate>
@property (nonatomic, retain)MBProgressHUD *hud;
@end

@implementation ActDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIScrollView *scrollV = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:scrollV];
    [scrollV release];
    
    scrollV.contentSize = CGSizeMake(375, 1800);

    ActDetailCell *actDeC = [[ActDetailCell alloc]init];
    actDeC.actLabel.text = self.myTitle;
    actDeC.actLabel.numberOfLines = 0;
    [actDeC.actLabel sizeToFit];
    actDeC.actMoldDataLabel.text = self.myMold;
    actDeC.actAddressLabel.text = self.myAddress;
    actDeC.actAddressLabel.numberOfLines = 0;
    [actDeC.actAddressLabel sizeToFit];
    actDeC.actTimeLabel.text = [NSString stringWithFormat:@"%@--%@",self.str,self.str1];
    actDeC.Abstract.text = self.myContent;
    actDeC.Abstract.numberOfLines = 0;
    
#pragma mark -- scroll自适应高度
    
    [actDeC.Abstract sizeToFit];
    CGFloat height = actDeC.Abstract.frame.origin.y + actDeC.Abstract.frame.size.height + 70;
    scrollV.contentSize = CGSizeMake(0, height);
    
    NSString *pp = [self.myOnwer objectForKey:@"name"];
    actDeC.actAdnamelabel.text = pp;
    
    NSURL *url = [NSURL URLWithString:self.myImage];
    [actDeC.actImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"e8db9451737015bf9622f88a14cc9a40.jpg"]];
    
    self.navigationItem.title = self.myTitle;
    
    
    
    [scrollV addSubview:actDeC];
    [actDeC release];
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back.png"];
#pragma mark -- 取消图片渲染
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *rightImage = [UIImage imageNamed:@"btn_nav_share.png"];
    
    rightImage = [rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
     self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:rightImage style:UIBarButtonItemStylePlain target:self action:@selector(RightAction:)];
    
    
}

- (void)leftButtonAction:(UIBarButtonItem *)btn{
    
    [self.navigationController popViewControllerAnimated:YES];
   
    
    
}

- (void)RightAction:(UIBarButtonItem *)btn{
     NSString *isLogin = [[NSUserDefaults standardUserDefaults] objectForKey:@"isLogin"];
    if ([isLogin isEqualToString: @"0"]) {
        
        LoginViewController *logVC = [[LoginViewController alloc]init];
    [self.navigationController pushViewController:logVC animated:YES];
    }else{
        
        
        
        NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"];
        
   NSMutableArray *array = [[SingLeton shareSingleton] selectActName:name];
        BOOL result = [array containsObject:self.myTitle];
        if (result) {
            
            self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.hud.labelText = @"已收藏过了";
            self.hud.mode = MBProgressHUDModeText;
            self.hud.delegate = self;
            [self.hud hide:YES afterDelay:1];
            
            
        }else{
            
               ActiveModel *act = [[ActiveModel alloc]init];
        act.UserActName = name;
        act.MovieName = self.myTitle;
            [[SingLeton shareSingleton] insertAct:act];
     
            
        
 
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }];
        [alert addAction: action];
        
        
    }
 }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
